﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using EasyCLRBase;
using EasyCLRBase.AssemblyExplorer;

namespace EasyCLRMisc
{
    [Export(typeof(EasyCLRBase.IClrBase))]
    [Serializable]
    [SqlUserDefinedTypeAttribute(Format.Native, Name = "Point", IsByteOrdered = true)]
    public struct Point : INullable, IClrBase
    {
        private bool _isNull;
        private double _X;
        private double _Y;

        public bool IsNull
        {
            get { return (this._isNull); }
        }

        public override string ToString()
        {
            return this.IsNull == true ? "NULL" : this._X + ":" + this._Y;
        }

        [SqlMethod(OnNullCall = false)]
        public static Point Parse(SqlString str)
        {
            Point pt = Point.Null;
            if (str.IsNull == false)
            {
                // Parse input string here to separate out points:
                pt = new Point();
                string[] xy = str.Value.Split(':');

                pt.X = Convert.ToDouble(xy[0]);
                pt.Y = Convert.ToDouble(xy[1]);
            }

            return pt;
        }

        public static Point Null
        {
            get
            {
                return new Point() { _isNull = true };
            }
        }

        public Double X
        {
            [SqlMethod(IsDeterministic = true)]
            get { return (this._X); }
            set { this._X = value; }
        }

        public Double Y
        {
            [SqlMethod(IsDeterministic = true)]
            get { return (this._Y); }
            set { this._Y = value; }
        }

        public ClrExportString GetClrCreationCode()
        {
            return ClrBase.GetClrCreationCode(this.GetType());
        }

        public String HowTo()
        {
            throw new NotImplementedException();
        }
    }
}
